package com.baixun.controller;

import com.baixun.constant.MessageConstant;
import com.baixun.entity.Result;
import com.baixun.pojo.Dep;
import com.baixun.service.DepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dep")
public class DepController {

    @Autowired
    private DepService depService;

    /**
     * 新增部门
     * @param dep
     * @return
     */
    @PostMapping("add")
    public Result addDep(@RequestBody Dep dep){

        try {
            depService.addDep(dep);
            return new Result(true,MessageConstant.ADD_DEP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_DEP_FAIL,e.getMessage());
        }
    }


    /**
     * 更新部门
     * @param dep
     * @return
     */
    @PutMapping("update")
    public Result updateDep(@RequestBody Dep dep){

        try {
            depService.upate(dep);
            return new Result(true, MessageConstant.EDIT_DEP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_DEP_FAIL,e.getMessage());
        }
    }

    /**
     * 永久删除部门
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteDepById(@PathVariable("id") Integer id){

        try {
            depService.deleteDepById(id);
            return new Result(true,MessageConstant.DELETE_DEP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_DEP_FAIL,e.getMessage());

        }
    }

    /**
     * 更新部门状态
     * @param id
     * @param dep_status
     * @return
     */
    @PatchMapping("/updatestatus/{id}/{status}")
    public Result updateDepStatus(@PathVariable("id") Integer id,@PathVariable("status") Byte dep_status){

        try {
            depService.updateDepStatus(id,dep_status);
            return new Result(true,MessageConstant.UPDATE_DEPSTATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_DEPSTATUS_FAIL,e.getMessage());

        }
    }

    /**
     * 查询一个部门，如果此部门有下级部门一同查询出来
     * @return
     */
    @GetMapping("find/{id}")
    public Result findById(@PathVariable Integer id){

        try {
            Map<String,Object> map = depService.findById(id);
            return new Result(true,MessageConstant.QUERY_DEP_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_DEP_FAIL,e.getMessage());
        }
    }

    /**
     * 查询所有部门，按照树形结构查询出来
     * @return
     */
    @GetMapping("find")
    public Result findAll(){

        try {
            List<Map<String,Object>> list = depService.findAll();
            return new Result(true,MessageConstant.QUERY_DEP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_DEP_FAIL,e.getMessage());
        }
    }

    /**
     * 部门添加用户
     */
    @PostMapping("/addUserToDep")
    public Result addUserToDep(@RequestBody Map map){

        try {
            depService.addUserToDep(map);
            return new Result(true,MessageConstant.ADD_USER_DEP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(false,MessageConstant.ADD_USER_DEP_FALT,e.getMessage());
        }
    }

    /**
     * 部门移除用户
     * @param map
     * @return
     */
    @DeleteMapping("/removeUserFromDep")
    public Result removeUserFromDep(@RequestBody Map map){

        try {
            depService.removeUserFromDep(map);
            return new Result(true,MessageConstant.REMOVE_USER_DEP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.REMOVE_USER_DEP_FALT,e.getMessage());
        }
    }

}
